package net.dgg.dggcloud.service.jrcrm.aspect;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import net.dgg.dggcloud.core.json.JsonUtil;
import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.core.util.RequestUtils;
import net.dgg.dggcloud.service.common.base.SessionManager;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.iboss.constant.SysRecordConstant;
import net.dgg.dggcloud.service.jrcrm.service.log.SysExportRecordService;
import net.dgg.dggcloud.service.jrcrm.utils.IpUtil;
import net.dgg.framework.tac.mq.rabbitmq.queue.DggMessageSendUtil;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;

/**
 * 导出日志aop
 * 
 * @Title: RoleAuthOptSaveAspect2
 * @Description:
 * @Auther: 雷力
 * @Date: 2018-11-23 09:49:44
 *
 */
@Aspect
@Component
public class ExportRecordAspect {
	private Logger logger = Logger.getLogger(ExportRecordAspect.class);

	@Pointcut("execution(public * net.dgg.dggcloud.web.action.jrcrm.business.BusinessRecordController.excelRecordList(..))")
	public void webLog() {
	}

	@Before("webLog()")
	public void before(JoinPoint jp) {
		try {
			ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = attributes.getRequest();
			User user = SessionManager.getCurrentUser();

			String dateString = DateUtil.dateToString(new Date(), "yyyy-HH-dd");
			String fileName = "已分配商机" + dateString + ".xls";

			Map<String, Object> opreate = new HashMap<>();
			opreate.put("page", request.getRequestURI());
			opreate.put("params", RequestUtils.getQueryParams(request));

			Map<String, Object> record = new HashMap<>();
			record.put("id", DggKeyWorker.nextId());
			record.put("userLoginname", user.getLoginName());
			record.put("userName", user.getName());
			record.put("opreateTime", DateUtil.dateToString(new Date()));
			record.put("ip", IpUtil.getIPAddress(request));
			record.put("opreate", JsonUtil.obj2Json(opreate));
			record.put("content", fileName);
			record.put("clazz", SysExportRecordService.class);
			logger.info("======================>发送消息...");
			DggMessageSendUtil.putMessage(SysRecordConstant.QUEUENAME, record);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
